<? if( $_SESSION[ "JC_account_authority" ] == "administrator" ) { ?>

	<b>MySQL</b><br /><hr />

	<table>
		<tr>
			<th>Stored Function</th>
			<th>List</th>
		</tr>
		<tr>
			<td><textarea cols="120" rows="8">

DELIMITER //



DROP FUNCTION IF EXISTS `JC_FK` //

CREATE FUNCTION `JC_FK`( `FK` text ) RETURNS int

	BEGIN

		RETURN ( LENGTH( `FK` ) - LENGTH( REPLACE( `FK`, '\r\n', '' ) ) ) / LENGTH( '\r\n' );

	END //

		<?

			$temp = "";

			foreach( $_SESSION[ "JC_scheme" ] as $key_table => $value_table ) {

				foreach( $_SESSION[ "JC_scheme" ][ $key_table ] as $key_field => $value_field ) {

					if( $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ] ) {

						if( strpos( $temp, "JC_FK_" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKT" ] . "." . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ] ) !== false ) continue;

						$temp .= "JC_FK_" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKT" ] . "." . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ];

						echo "

DROP FUNCTION IF EXISTS `JC_FK_" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKT" ] . "." . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ] . "` //

CREATE FUNCTION `JC_FK_" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKT" ] . "." . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ] . "`( `FK` text ) RETURNS text

	BEGIN

		DECLARE `count`		int;
		DECLARE `record`	text;
		DECLARE `result`	text;

		SET `count` = JC_FK( `FK` ) + 1;

		WHILE( `count` > 0 ) DO

			SET `record` = '';

			SELECT	`" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ] . "`
			FROM	`" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKT" ] . "`
			WHERE	`key` = SUBSTRING_INDEX( SUBSTRING_INDEX( `FK`, '\\r\\n', - `count` ), '\\r\\n', 1 )
			INTO	`record`;

			SET `result`	= CONCAT_WS( '', `result`, SUBSTRING_INDEX( SUBSTRING_INDEX( `FK`, '\\r\\n', - `count` ), '\\r\\n', 1 ), ': ', `record`, '\\r\\n' );
			SET `count`		= `count` - 1;

		END WHILE;

		RETURN TRIM( BOTH '\\r\\n' FROM `result` );

	END //

						";

						// The format of FKF can be considered as ':' for an assign character and '\r\n' for a separate character. 

					}

				}

			}

		?>



		<?

			foreach( $_SESSION[ "JC_scheme" ] as $key_table => $value_table ) {

				if( $_SESSION[ "JC_scheme" ][ $key_table ][ "parent" ] && $_SESSION[ "JC_scheme" ][ $key_table ][ "priority" ] ) {

					echo "

DROP FUNCTION IF EXISTS `JC_sort_" . $key_table . "` //

CREATE FUNCTION `JC_sort_" . $key_table . "`( `key_target` int ) RETURNS text

	BEGIN

		DECLARE `count`				int;
		DECLARE `record_key`		int;
		DECLARE `record_parent`		bigint;
		DECLARE `record_priority`	int;
		DECLARE `result`			text;

		SET `count`		= 8;
		SET `result`	= CONCAT( ( `key_target` + 5000000000 ), '; 9' );

		WHILE( `count` > 0 ) DO

			SELECT	`key`
			FROM	`task`
			WHERE	`key` = `key_target`
			INTO	`record_key`;

			SELECT	`parent`
			FROM	`task`
			WHERE	`key` = `key_target`
			INTO	`record_parent`;

			SELECT	`priority`
			FROM	`task`
			WHERE	`key` = `key_target`
			INTO	`record_priority`;

			IF `record_parent` IS NULL THEN

				SET `result`		= CONCAT( ( `record_priority` + 500 ), ': ', `result` );
				SET `count`			= 0;

			ELSE

				SET `result`		= CONCAT( ( `record_parent` + 5000000000 ), '; ', ( `record_priority` + 500 ), ': ', `result` );
				SET `key_target`	= `record_parent`;
				SET `count`			=  `count` - 1;

			END IF;

		END WHILE;

		RETURN `result`;

	END //

					";

					// This can cause a halt.

				}

			}

		?>



DELIMITER ;

		</textarea></td>
		<td>

			JC_FK<br />

			<?

				$temp = "";

				foreach( $_SESSION[ "JC_scheme" ] as $key_table => $value_table ) {

					foreach( $_SESSION[ "JC_scheme" ][ $key_table ] as $key_field => $value_field ) {

						if( $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ] ) {

							if( strpos( $temp, "JC_FK_" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKT" ] . "." . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ] ) !== false ) continue;

							$temp .= "JC_FK_" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKT" ] . "." . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ];

							echo "JC_FK_" . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKT" ] . "." . $_SESSION[ "JC_scheme" ][ $key_table ][ $key_field ][ "FKF" ] . "<br />";

						}

					}

				}

				foreach( $_SESSION[ "JC_scheme" ] as $key_table => $value_table ) {

					if( $_SESSION[ "JC_scheme" ][ $key_table ][ "parent" ] && $_SESSION[ "JC_scheme" ][ $key_table ][ "priority" ] ) echo "JC_sort_" . $key_table . "<br />";

				}

			?>

		</td>
	</tr>
</table>



<? } ?>
